package com.smartwebee.android.blespp.hospital.fragment;


import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.google.gson.Gson;
import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.bean.MsgBean;
import com.smartwebee.android.blespp.databinding.FragmentMessageBinding;
import com.smartwebee.android.blespp.databinding.FragmentMineBinding;
import com.smartwebee.android.blespp.hospital.MsgDetailActivity;
import com.smartwebee.android.blespp.hospital.adapter.MsgAdapter;
import com.smartwebee.android.blespp.http.HttpRequest;
import com.smartwebee.android.blespp.http.RequestInterface;
import com.smartwebee.android.blespp.http.VolleyReqQueue;
import com.smartwebee.android.blespp.resp.MsgResp;
import com.smartwebee.android.blespp.utils.UIUtils;
import com.smartwebee.android.blespp.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class MessageFragment extends BaseFragment implements RequestInterface {

    private FragmentMessageBinding binding;
    private MsgAdapter msgAdapter;

    public MessageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_message, container, false);
        TAG = "MessageFragment";
        requestQueue = VolleyReqQueue.getInstance(getActivity()).getRequestQueue();
        return binding.getRoot();
    }

    private void httpGetMsgList() {
        showLoading();
        HttpRequest httpRequest = new HttpRequest(Request.Method.POST, Utils.getMessageListBySn,
                this) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("sn", Utils.registerResp.getData().getSn());
                return map;
            }
        };
        requestQueue.add(httpRequest);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        msgAdapter = new MsgAdapter(getActivity(), new ArrayList<MsgBean>());
        binding.listview.setAdapter(msgAdapter);

        binding.listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MsgBean bean = (MsgBean)parent.getAdapter().getItem(position);
                Intent intent = new Intent(getActivity(), MsgDetailActivity.class);
                intent.putExtra("title", bean.getTitle());
                intent.putExtra("content", bean.getContent());
                intent.putExtra("id", bean.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        httpGetMsgList();
    }

    @Override
    public void onSuccess(String id, String resp) {
        dismissLoading();
        MsgResp msgResp = new Gson().fromJson(resp, MsgResp.class);
        final List<MsgBean> beans = new ArrayList<>();
        if (Utils.parseData(msgResp)) {
            for (int i = 0; i < msgResp.getData().size(); i++) {
                beans.add(new MsgBean(msgResp.getData().get(i).getTitle(), msgResp.getData().get(i).getContent(), msgResp.getData().get(i).getId(), msgResp.getData().get(i).getCreateTime()));
            }
            if (beans.size() > 0) {
                binding.listview.setVisibility(View.VISIBLE);
                binding.tvNodata.setVisibility(View.GONE);
                msgAdapter.setItems(beans);
            } else {
                binding.listview.setVisibility(View.GONE);
                binding.tvNodata.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onError(String error) {
        dismissLoading();
        UIUtils.showTips(getActivity(), error);
    }
}
